package com.zy.algorithm.controller.screen;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zy.algorithm.R;
import com.zy.algorithm.bean.SortStepBean;
import com.zy.algorithm.controller.data.DataAndColorController;
import com.zy.algorithm.curtain.BaseChildScreenBean;
import com.zy.algorithm.fragment.BaseAlgorithmFragment;
import com.zy.utils.ScreenChangeUtils;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseChildScreenController extends BaseScreenController {

    protected final Context context;
    protected final Resources resources;
    FrameLayout root;
    LinearLayout child_data_container;
    TextView pk_op;

    List<TextView> textViewList = new ArrayList<>();

    protected final DataAndColorController dataController;

    public BaseChildScreenController(FrameLayout root) {
        super(root.getContext());
        this.context = root.getContext();
        this.resources = context.getResources();
        this.root = root;

        dataController = new DataAndColorController(textViewList, resources, true);

        View view = LayoutInflater.from(context).inflate(R.layout.base_screen_child_container, root);
        child_data_container = view.findViewById(R.id.base_screen_child_data_container);
        pk_op = view.findViewById(R.id.base_screen_child_anchor_point);
    }

    public void setScreenBean(BaseChildScreenBean bean) {
        int[] subSetStart = bean.getSubSetStart();
        for (int index = 0; index < subSetStart.length; index++) {
            TextView textView = buildDataTextView(subSetStart[index]);
            child_data_container.addView(buildEmptyView());
            child_data_container.addView(textView);
            textViewList.add(textView);

            if (index == subSetStart.length - 1) {
                child_data_container.addView(buildEmptyView());
            }
        }
        root.setVisibility(View.INVISIBLE);
    }

    public void finish() {
        root.removeAllViews();
        textViewList.clear();
    }

    private void updateOriginViews(List<View> originListView, boolean show) {
        for (View v : originListView) {
            v.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
        }
    }


    public void startPageEnterAnimator(BaseBallFirstScreenController firstScreenController, BaseChildScreenBean bean, List<View> originListView, BaseAlgorithmFragment.StepListener listener) {
        List<ScreenChangeUtils.PointNode> pointNodes = new ArrayList<>();

        for (int i = 0; i < originListView.size(); i++) {
            pointNodes.add(new ScreenChangeUtils.PointNode(originListView.get(i), textViewList.get(i)));
        }

        ScreenChangeUtils.screenOutOrInAnimator(root, pointNodes, true, new ScreenChangeUtils.ScreenChangeListener() {
            @Override
            public void screenEnd() {
                //进入动画结束-隐藏origin Views
                updateOriginViews(originListView, false);


                //开始排序动画
                startSubCartoon(bean.getCartoonStepBeanList(), new BaseAlgorithmFragment.StepListener() {
                    @Override
                    public void nextStep() {
                        //screen退出动画开始-展示origin Views
                        updateOriginViews(originListView, true);
                        //page退出动画-start
                        firstScreenController.setScreenData(bean, false);
                        startPageOutAnimator(pointNodes, listener);
                    }
                });
            }
        });
    }

    private void startPageOutAnimator(List<ScreenChangeUtils.PointNode> pointNodes, BaseAlgorithmFragment.StepListener listener) {

        ScreenChangeUtils.screenOutOrInAnimator(root, pointNodes, false, new ScreenChangeUtils.ScreenChangeListener() {
            @Override
            public void screenEnd() {
                //page退出动画-end
                finish();
                //然后是下一幕的动画
                listener.nextStep();
            }
        });
    }

    @Override
    protected View[] getDataViewS() {
        View[] views = new View[textViewList.size()];
        return textViewList.toArray(views);
    }

    @Override
    protected View getDataViewByIndex(int index) {
        return textViewList.get(index);
    }


    @Override
    public void setSortData(SortStepBean bean, boolean start) {
        dataController.setSortData(bean, start);
    }

    protected void setOpIndexStatues(SortStepBean bean) {
        dataController.setDataItemRed(bean.getOpFirstIndex());
        dataController.setDataItemRed(bean.getOpSecondIndex());
    }

    @Override
    void playStepAnimation(int index, SortStepBean bean, BaseAlgorithmFragment.StepListener stepListener) {
        if (checkActivityDestroyed()) {
            return;
        }

        //设置当前操作位置
        setOpIndexStatues(bean);

        //数据动画
        sortDataAnimation(bean, stepListener);
    }

    private void sortDataAnimation(SortStepBean curStepBean, BaseAlgorithmFragment.StepListener listener) {
        if (checkActivityDestroyed()) {
            return;
        }
        //串行操作
        showPK(curStepBean, listener);
    }


    abstract void showPK(SortStepBean curStepBean, BaseAlgorithmFragment.StepListener listener);
}
